//
//  ConnectUsViewController.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/9.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "ConnectUsViewController.h"

#import "XXTool.h"

@interface ConnectUsViewController ()<UITextViewDelegate>

@property (nonatomic,strong) UIScrollView *content;

@property (nonatomic,strong) UILabel *topLabelShow;

@property (nonatomic,strong) UIView *inputBackground;

@property (nonatomic,strong) UITextView *yourQuestion;

@property (nonatomic,strong) VerticalSettingLabel *textFieldPlade;

@property (nonatomic,strong) DKButton *send;

@property (nonatomic,strong) NSMutableArray *labelArray;

@property(nonatomic) NSInteger total;

@end

@implementation ConnectUsViewController
{
    NSMutableArray *bottomLabelArray;
}
-(UIScrollView*)content
{
    if (_content==nil)
    {
        _content = [[UIScrollView alloc] init];
        [self.view addSubview:_content];
    }
    return _content;
}

-(UILabel*)topLabelShow
{
    if (_topLabelShow==nil)
    {
        _topLabelShow = [[UILabel alloc] init];
        _topLabelShow.backgroundColor = [UIColor clearColor];
        _topLabelShow.font = [XXTool systemFontOfSize:15];
        _topLabelShow.text = NSLocalizedString(@"问题与意见", nil);
        _topLabelShow.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1.0];
        [_content addSubview:_topLabelShow];
    }
    return _topLabelShow;
}

-(UIView*)inputBackground
{
    if (_inputBackground==nil)
    {
        _inputBackground = [[UIView alloc] init];
        _inputBackground.backgroundColor = [UIColor whiteColor];
        [_content addSubview:_inputBackground];
    }
    return _inputBackground;
}

-(UITextView*)yourQuestion
{
    if (_yourQuestion==nil)
    {
        _yourQuestion = [[UITextView alloc] init];
        _yourQuestion.returnKeyType = UIReturnKeyDone;
        _yourQuestion.font = [XXTool systemFontOfSize:17];
        _yourQuestion.delegate = self;
        _yourQuestion.returnKeyType = UIReturnKeyDone;
        _yourQuestion.backgroundColor =[UIColor whiteColor];
        [_content addSubview:_yourQuestion];
    }
    return _yourQuestion;
}

-(VerticalSettingLabel*)textFieldPlade
{
    if (_textFieldPlade==nil)
    {
        _textFieldPlade = [[VerticalSettingLabel alloc] init];
        _textFieldPlade.text = NSLocalizedString(@"请简要描述您的问题与意见", nil);
        _textFieldPlade.numberOfLines = 0;
        _textFieldPlade.verticalAlignment = VerticalAlignmentTop;
        _textFieldPlade.textColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1.0];
        _textFieldPlade.font = [XXTool systemFontOfSize:17];
        [_content addSubview:_textFieldPlade];
    }
    return _textFieldPlade;
}

-(DKButton*)send
{
    if (_send==nil)
    {
        _send = [[DKButton alloc] init];
        _send.text = NSLocalizedString(@"发送", nil);
        [_send setColor:[XXTool colorWithProject] ForState:DKButtonStateNormal];
        _send.textColor = [UIColor whiteColor];
        _send.layer.cornerRadius = 5;
        [_send addTarget:self Select:@selector(clickSendButton) ForEvent:DKButtonEventPressedEnd];
        [_content addSubview:_send];
    }
    return _send;
}

-(NSMutableArray*)labelArray
{
    if (_labelArray==nil)
    {
        _labelArray = [[NSMutableArray alloc] init];
        for (int i=0; i<6; i++)
        {
            UILabel *temp = [[UILabel alloc] init];
            temp.font = [XXTool systemFontOfSize:15];
            temp.textAlignment = NSTextAlignmentCenter;
            temp.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1.0];
            temp.numberOfLines = 0;
            
            if (i==0)
            {
                temp.text = NSLocalizedString(@"华汉国际会议展览(上海)有限公司", nil);

            }
            else if (i==1)
            {
                temp.text = NSLocalizedString(@"中国上海仙霞路318-322号鑫达大厦2402室", nil);

            }
            else if (i==2)
            {
                temp.text = [NSString stringWithFormat:@"%@: 200336",NSLocalizedString(@"邮编", nil)];

            }
            else if (i==3)
            {
                temp.text = [NSString stringWithFormat:@"%@: +86 21 6209 5209",NSLocalizedString(@"电话", nil)];

            }
            else if (i==4)
            {
                temp.text = [NSString stringWithFormat:@"%@: +86 21 6209 5210",NSLocalizedString(@"传真", nil)];
            }
            else if (i==5)
            {
                temp.text = [NSString stringWithFormat:@"%@: propak@chinaallword.com",NSLocalizedString(@"电子邮件", nil)];
            }
            
            [_content addSubview:temp];
            [_labelArray addObject:temp];
        }
    }
    return _labelArray;
}

#pragma mark sub
-(void)initParameter
{
    bottomLabelArray = [[NSMutableArray alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged) name:UITextViewTextDidChangeNotification object:nil];
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"联系我们", nil)];
    [self.view setBackgroundColor: [XXTool colorWithBackGround]];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(clickleft)]];
    
}

-(void)refreshLayout:(CGRect)frame
{
    self.content.frame = CGRectMake(0, 0, frame.size.width
                                    , frame.size.height);
    
    self.topLabelShow.frame = CGRectMake(20
                                         , 0
                                         , frame.size.width-20, 28);
    self.inputBackground.frame = CGRectMake(0, _topLabelShow.frame.origin.y+_topLabelShow.frame.size.height
                                            , frame.size.width
                                            , 145);
    self.yourQuestion.frame = CGRectMake(20, _topLabelShow.frame.origin.y+_topLabelShow.frame.size.height+6
                                         , frame.size.width-40
                                         , 145);
    self.textFieldPlade.frame = CGRectMake(_yourQuestion.frame.origin.x
                                           , 15+_topLabelShow.frame.origin.y+_topLabelShow.frame.size.height
                                           , _yourQuestion.frame.size.width
                                           , _yourQuestion.frame.size.height-30);
    self.send.frame = CGRectMake(_yourQuestion.frame.origin.x
                                 , _inputBackground.frame.origin.y+_inputBackground.frame.size.height+18
                                 , frame.size.width-40
                                 , 38);
    for (int i=0;i<self.labelArray.count; i++)
    {
        UILabel *temp = [_labelArray objectAtIndex:i];
        temp.frame = CGRectMake(10
                                , _send.frame.size.height+_send.frame.origin.y+100+i*12+_total
                                , frame.size.width-20
                                , [XXTool getTextHeightByText:temp.text Font:temp.font Width:frame.size.width-20]);
//        if(i >=2 && ![XXTool didCurrentLanguageChinese ]){
//            temp.frame = CGRectMake(10
//                                    , _send.frame.size.height+_send.frame.origin.y+160+i*(12+22)
//                                    , frame.size.width-20
//                                    , [XXTool getTextHeightByText:temp.text Font:temp.font Width:frame.size.width-20]);
//        }else{
//            temp.frame = CGRectMake(10
//                                    , _send.frame.size.height+_send.frame.origin.y+160+i*(12+17)
//                                    , frame.size.width-20
//                                    , [XXTool getTextHeightByText:temp.text Font:temp.font Width:frame.size.width-20]);
//        }
        
        
        if (i==_labelArray.count-1)
        {
            _content.contentSize = CGSizeMake(frame.size.width
                                              , temp.frame.size.height+temp.frame.origin.y+30);
        }
        _total+=temp.frame.size.height;
    }
}


-(void)textChanged
{
    if(_yourQuestion.text.length>0)
    {
        _textFieldPlade.hidden = YES;
    }
    else if(_yourQuestion.text.length==0)
    {
        _textFieldPlade.hidden = NO;
    }
    
}

-(void)clickSendButton
{
    if ([_yourQuestion.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"请输入您宝贵的意见", nil)];
        return;
    }
    if ([_yourQuestion.text length]>300)
    {
        [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"输入内容必须在300字符内", nil)];
        return;
    }
    [DKHTTPRequest sendDataWithShortUrl:@"contact_us"
                                  Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=%@&passwd=%@&content=%@",[UserInfo shareUserInfo].userID,[UserInfo shareUserInfo].userPassword,_yourQuestion.text]]
                             ImageArray:nil
                                TimeOut:10
                             Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                 if ([[serverDic objectForKey:@"code"] intValue]==200)
                                 {
                                     [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"发送成功", nil)];
                                     _yourQuestion.text = @"";
                                     _textFieldPlade.hidden = NO;
                                 }
                                 return YES;
                             } IsBuffering:NO IsNeedReplaceWrap:NO IsUseUpLoadHead:NO];
}

#pragma mark UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
    
}

-(void)clickleft
{
    //[self.navigationController.sideViewController showLeftView];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
